<?php

namespace common\helpers;

/**
 * ColorHelper.
 *
 * @author emhome <emhome@163.com>
 * @since 2.0
 */
class ColorHelper {

    /**
     * 格式化颜色值
     * 
     * @param mixed $color 
     * @return array
     */
    public static function convertValidateHex($color, $default = '000000', $prepend = '#') {
        $hex = '';
        if (is_string($color)) {
            if (strpos($color, '#') !== false) {
                $color = str_replace(['#', ' '], '', $color);
                $lens = strlen($color);
                if ($lens % 3) {
                    return false;
                }
                if ($lens < 6) {
                    for ($i = 0; $i < $lens; $i++) {
                        $hex .= str_repeat($color[$i], 2);
                    }
                }
                $hex = substr($color, 0, 6);
            } else {
                $temp = preg_split("/[\s,]+/", $color);
                $rgb = array_pad($temp, 3, 0);
                for ($i = 0; $i < 3; $i++) {
                    $v = $rgb[$i];
                    if ($v > 255) {
                        $v = 255;
                    } elseif ($v < 0) {
                        $v = 0;
                    }
                    $hex .= dechex($v);
                }
            }
        } elseif (is_array($color)) {
            $rgb = array_pad($color, 3, 0);
            foreach ($rgb as $v) {
                if ($v > 255) {
                    $v = 255;
                } elseif ($v < 0) {
                    $v = 0;
                }
                $hex .= dechex($v);
            }
        } else {
            $hex = $default;
        }
        return $prepend ? $prepend . $hex : $hex;
    }

    /**
     * 格式化颜色值
     * 
     * @param mixed $color 
     * @return array
     */
    public static function format($color, $type = null, $default = '000000') {
        $hex = static::convertValidateHex($color, $default);
        if ($type == 'hex') {
            return $hex;
        } elseif ($type == 'rgb') {
            return static::hex2rgb($hex);
        }
        return [
            'rgb' => static::hex2rgb($hex),
            'hex' => $hex,
        ];
    }

    /**
     * 十六进制转RGB
     * 
     * @param string $color 16进制颜色值
     * @return array
     */
    public static function rgb2hex($color) {
        if (!is_array($color)) {
            return false;
        }
        $rgb = array_pad($color, 3, 0);
        $hex = [];
        foreach ($rgb as $v) {
            if ($v > 255) {
                $v = 255;
            } elseif ($v < 0) {
                $v = 0;
            }
            $hex[] = dechex($v);
        }
        return implode('', $hex);
    }

    /**
     * 十六进制转RGB
     * 
     * @param string $color 16进制颜色值
     * @return array
     */
    public static function hex2rgb($color) {
        $hexColor = str_replace('#', '', $color);
        $lens = strlen($hexColor);
        if ($lens != 3 && $lens != 6) {
            return false;
        }
        $newcolor = '';
        if ($lens == 3) {
            for ($i = 0; $i < $lens; $i++) {
                $newcolor .= $hexColor[$i] . $hexColor[$i];
            }
        } else {
            $newcolor = $hexColor;
        }
        $hex = str_split($newcolor, 2);
        $rgb = [];
        foreach ($hex as $key => $vls) {
            $rgb[] = hexdec($vls);
        }
        return $rgb;
    }

}
